var cart = {

    init: function() {

        E.ajax_get({
            action: 'cart',
            operFlg: 6,
            call: function( o ) {
                cart.display( o.data );

                //add by dongleixin 判断是否点击了修改按钮 start
                if(is_show == 1){
                    cart.show(is_show);
                }else if(is_show == 2){
                    cart.show(is_show);
                }else{
                    $('.numMinus').removeClass('p_left');
                    $('.numAdd').removeClass('p_right');
                    $('.cake_num dl dd').css('background-color','transparent');
                }
                //add by dongleixin 判断是否点击了修改按钮 end
            }
        });

    }, 

    //点击修改购物车显示选择框和删除按钮
    show: function(sid){
        if(sid == 1){
            is_show  = 1;
            $('.store').css('display','block');
            $('.all_cur').css('display','block');
            $('.all_rodio_txt').text('全选');
            $('.showBtn').html('<button id="showBtn" class="btn btn-primary" style="width: 102px;" onclick="cart.show(2);" type="button">完成</button>');
            $('.spec').css('display','block');
            $('.pound').css('display','none');
            $('.numMinus').addClass('p_left');
            $('.numAdd').addClass('p_right');
            $('.cake_num dl dd').css('background-color','#fff');
        }else if(sid == 2){
            is_show  = 2;
            $('.store').css('display','none');
            $('.all_cur').css('display','none');
            $('.all_rodio_txt').text('');
            $('.showBtn').html('<button id="showBtn" class="btn btn-primary" style="width: 102px;" onclick="cart.show(1);" type="button">修改购物车</button>');
            $('.spec').css('display','none');
            $('.pound').css('display','block');
            $('.numMinus').removeClass('p_left');
            $('.numAdd').removeClass('p_right');
            $('.cake_num dl dd').css('background-color','transparent');
        }

    },
    display: function( data ) {

        if ($(data.goods).length == 0) {
            E.refresh();
            return false;
        }

        cart.specArr = data.spec;
        var is_all_buy = 1;

        var html = '';

        html += '<div class="pro_list">';
        html += '<div class="rodio_box" style="float:left">';
        html += '<div class="all_rodio cartCheck">';
        html += '<a href="javascript:void(0);"><div class="all_cur" style="display: none;"><span></span></div></a>';
        html += '<input type="radio" hidden="hidden" >';
        html += '</div>';
        html += '</div>';
        html += '<span class="all_rodio_txt"></span>';
        html += '<div class="showBtn" style="float: right; margin-top: -7px;">';
        html += '<button id="showBtn" class="btn btn-primary" style="width: 102px;" onclick="cart.show(1);" type="button">修改购物车</button>';
        html += '</div>';
        html += '</div>';

        $.each(data.goods, function(k, goods) {
            var fittings_text = '';
            if (goods.fittings) {
                $.each(goods.fittings, function(i, fittings) {
                    if (fittings.forsale == 1) {
                        return true;
                    }
                    if (fittings_text == '') {
                        fittings_text += fittings.fitName + fittings.quantity + fittings.unitName;
                    } else {
                        fittings_text += '&nbsp;&nbsp;' + fittings.fitName + fittings.quantity + fittings.unitName;
                    }
                });
            }

            html += '<div class="pro_list">';
            html += '<div class="store" style="display: none;">';
            html += '<div class="rodio_box">';
            html += '<div class="Choice_new cartCheck">';

            if(goods.is_buy == 1){
                html += '<a href="javascript:void(0);"><div class="cur"><span></span></div>';
            }else{
                is_all_buy = 0;
                html += '<a href="javascript:void(0);"><div><span></span></div>';
            }

            html += '<input type="radio"  hidden="hidden" value="'+ goods.postID +'" ></a>';
            html += '</div>';
            html += '</div> ';
            html += '<a href="javascript: void(0);" class="del pro_del" index="'+ k +'"></a></div>';
            html += '<div class="pro_box">';
            html += '<div class="pro_img">';
            html += '<a href="'+ goods.wap_link +'">';
            html += '<img src="'+ goods.img_link +'" width="100%" alt="'+ goods.goodsName +'">';
            html += '</a>';
            html += '</div>';
            html += '<div class="pro_right">';
            html += '<div>';
            html += '<p class="pro_name">'+ goods.goodsName +'</p>';
            html += '<span>'+ goods.price.toFixed(2) +'</span>';
            html += '</div>';
            html += '<div class="amount">';
            html += '<font>磅数</font>';
            html += '<input name="orderSpec" type="hidden" value="'+ goods.spec +'">';
            html +='<select class ="spec" name="spec" style="width: 111px;margin-left: 39px;display: none;">';
            if (goods.specArr) {
                $.each(goods.specArr, function(i,v) {
                    var selected = '';
                    if( goods.postID == i){
                        var selected = "selected";
                    }
                    html +='<option value="'+ v +'" '+selected+'>'+ v +'</option>';
                });
            }
            html +='</select>';
            html +='<span class="pound" style="margin-left: 42px;">'+ goods.spec +'</span>';
            html += '</div>';

            html += '<div class="cake_num">';
            html += '<font>数量</font>';
            html += '<dl>';
            if (goods.goods_amount == 0) {
                html += '<dd class="p_left nopoint numMinus"></dd>';
            } else {
                html += '<dd class="p_left numMinus"></dd>';
            }
            html += '<dt><input name="" type="text" value="'+ goods.goods_amount +'" class="goods_amount" index="'+ k +'"></dt>';
            html += '<dd class="p_right numAdd"></dd>';
            html += '</dl>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            if(fittings_text){
                html += '<p class="Remind">赠送：'+ fittings_text +'</p>';
            }
            html += '</div>';
        });

        $('#buy_car_list').html(html);

        if(is_all_buy == 1){
            $(".all_rodio div").addClass('cur');
        }else{
            $(".all_rodio div").removeClass('cur');
        }

        G.cart.comm.get_amount("cart_amount");
        $('#total_money').text(' ￥' + data.is_buy_total_money.toFixed(2));
        if (data.is_buy_total_amount == 0) {
            $('#checkout').hide();
        }else{
            $('#checkout').show();
        }

    },

    clearing:function(){

        E.loadding.open('正在检查购物车，请稍候...');
        E.ajax_get({
            action: 'cart',
            operFlg: 12,
            data: { },
            call: function( o ) {
                E.loadding.close();

                if(!o){
                    return false;
                }

                if(o.code == 200){
                    cart.errCount = o.data.errCount;
                }else{
                    cart.errCount = 0;
                }

                if(cart.errCount == 0){
                    cart.nowbuy_login_go();
                }else{
                    E.confirm(o.data.errMessage, "cart.nowbuy_login_go", "cart.refresh_cart");
                }
            }
        });
    },

    //跳转至立刻购买
    nowbuy_login_go:function(){

        if(cart.errCount <= 1){
            self.location = G.args.nowbuy_login + '?act=cart';
        }else{
            cart.refresh_cart();
        }

    },

    //刷新购物车
    refresh_cart:function(){
        cart.init();
    }


};

if (is_init_cart) {
    cart.init();
}

//add by dongleixin 定义修改按钮是否被点击变量 0是没点击，1是点击 start
var is_show = 0;
//add by dongleixin 定义修改按钮是否被点击变量 0是没点击，1是点击 end

$(window).ready(function() {

    //删除购物车商品
    $(document).on('click', '.pro_del', function() {

        //add by dongleixin 判断是否点击了修改按钮 start
        if(is_show == 0){
           return false;
        }
        //add by dongleixin 判断是否点击了修改按钮 end
        var index = $(this).attr('index');

        E.confirm('您确认要删除该商品吗？', function() {

            E.loadding.open('正在删除，请稍候...');
            E.ajax_get({
                action: 'cart',
                operFlg: 4,
                data: {
                    index: index
                },
                call: function( o ) {
                    E.loadding.close();
                    if (o.code == 200) {
                        cart.display( o.data.goods_list );
                        //add by dongleixin 判断是否点击了修改按钮 start
                        cart.init();
                        cart.show(is_show);
                        //add by dongleixin 判断是否点击了修改按钮 end
                        E.alert(o.message, 2);
                    } else {
                        E.alert(o.message);
                    }
                }
            });

        });

    });

    //加减按钮
    $(document).on('click', '.p_right', function() {
        //add by dongleixin 判断是否点击了修改按钮 start
        if(is_show == 0){
            return false;
        }
        //add by dongleixin 判断是否点击了修改按钮 end

        var num = $(this).parent().find('.goods_amount');
        var oldValue=parseInt(num.val()); //取出现在的值，并使用parseInt转为int类型数据

        if(oldValue >= 50){
            return false;
        }

        oldValue++;  //自加1
        num.val(oldValue);  //将增加后的值付给控件
        $('.p_left').removeClass('nopoint');
        E.ajax_get({
            action: 'cart',
            operFlg: 3,
            data: {
                index: num.attr('index'),
                goods_amount: oldValue
            },
            call: function( o ) {
                if (o.code == 200) {
                    cart.display( o.data.goods_list );
                    //add by dongleixin 判断是否点击了修改按钮 start
                        cart.init();
                        cart.show(is_show);
                    //add by dongleixin 判断是否点击了修改按钮 end
                }
            }
        });

    });

    $(document).on('click', '.p_left', function() {
        //add by dongleixin 判断是否点击了修改按钮 start
        if(is_show == 0){
            return false;
        }
        //add by dongleixin 判断是否点击了修改按钮 end
        if ($(this).hasClass('nopoint')) {
            return false;
        }
        var num = $(this).parent().find('.goods_amount');
        var oldValue=parseInt(num.val()); //取出现在的值，并使用parseInt转为int类型数据

        oldValue--;   //自减1
        if (oldValue == 0) {
            $(this).addClass('nopoint');
            return false;

        } else {
            num.val(oldValue);  //将增加后的值付给控件
            E.ajax_get({
                action: 'cart',
                operFlg: 3,
                data: {
                    index: num.attr('index'),
                    goods_amount: oldValue
                },
                call: function( o ) {
                    if (o.code == 200) {
                        cart.display( o.data.goods_list );
                        //add by dongleixin 判断是否点击了修改按钮 start
                        cart.init();
                        cart.show(is_show);
                        //add by dongleixin 判断是否点击了修改按钮 end
                    }
                }
            });
        }
    });

    //add by dongleixin 磅数更改 start
    $(document).on('change', '.spec', function() {
        if(is_show == 0){
            return false;
        }

        var oldSpec = $(this).siblings('input[name=orderSpec]').val();
        var newSpec = $(this).children('option:selected').val();

        var sepc = oldSpec.replace(/\d+/g,'');
        if(sepc != "磅"){
            return false;
        }

        var num = $(this).parent().parent().parent().parent().find('.goods_amount');
        var count = parseInt(num.val());

        E.ajax_get({
            action: 'cart',
            operFlg: 8,
            data: {
                index: num.attr('index'),
                goods_spec: newSpec,
                goods_amount: count
            },
            call: function( o ) {
                if (o.code == 200) {
//                    cart.display( o.data.goods_list );
                    cart.init();
                }else{
                    E.alert(o.message);
                }
            }
        });

    });


    //add by dongleixin 磅数更改 end

    var temp_goods_amount = 0;
    $(document).on('focus', '.goods_amount', function() {
        temp_goods_amount = E.trim($(this).val());
    });

    $(document).on('blur', '.goods_amount', function() {
        //add by dongleixin 判断是否点击了修改按钮 start
        if(is_show == 0){
            return false;
        }
        //add by dongleixin 判断是否点击了修改按钮 end
        var goods_amount = E.trim($(this).val());
        if (goods_amount == temp_goods_amount) {
            return false;
        } else if (goods_amount > 50) {
            $(this).val(temp_goods_amount);
            return false;
        } else if (!E.isDigital(goods_amount)) {
            $(this).val(temp_goods_amount);
            return false;
        }
        var index = $(this).attr('index');
        var _this = this;
        if (goods_amount <= 0) {
            E.confirm('您确认要删除该商品吗？', function() {

                E.loadding.open('正在删除，请稍候...');
                E.ajax_get({
                    action: 'cart',
                    operFlg: 4,
                    data: {
                        index: index
                    },
                    call: function( o ) {
                        E.loadding.close();
                        if (o.code == 200) {
                            cart.display( o.data.goods_list );
                            //add by dongleixin 判断是否点击了修改按钮 start
                            cart.init();
                            cart.show(is_show);
                            //add by dongleixin 判断是否点击了修改按钮 end
                            E.alert(o.message, 2);
                        } else {
                            E.alert(o.message);
                        }
                    }
                });

            }, function() {
                $(_this).val(temp_goods_amount);
            });
        } else {
            E.ajax_get({
                action: 'cart',
                operFlg: 3,
                data: {
                    index: index,
                    goods_amount: goods_amount
                },
                call: function( o ) {
                    if (o.code == 200) {
                        cart.display( o.data.goods_list );
                        //add by dongleixin 判断是否点击了修改按钮 start
                        cart.init();
                        cart.show(is_show);
                        //add by dongleixin 判断是否点击了修改按钮 end
                    }
                }
            });
        }
    });

    //购物车商品选择/全选/全取消
    $(document).on('click', '.cartCheck div', function(){

        var id, sign;

        if($(this).hasClass('cur')){
            sign = 0;
            if($(this).hasClass('all_cur')){
                id = 0;
                $(".cartCheck div").removeClass('cur');
            }else{
                id = $(this).next('input').val();
                $(this).removeClass('cur');
            }
        }else{
            sign = 1;
            if($(this).hasClass('all_cur')){
                id = 0;
                $(".cartCheck div").addClass('cur');
            }else{
                id = $(this).next('input').val();
                $(this).addClass('cur');
            }
        }

        E.loadding.open('正在加载中，请稍候...');
        E.ajax_post({
            action:'cart',
            operFlg:11,
            data:{
                id:id,
                sign:sign
            },
            call:function(o){

                E.loadding.close();
                if(o.code == 200){
                    cart.init();
                }else{
                    E.alert(o.message);
                }
            }
        });

    });


});
